<?php
defined( '_JEXEC' ) or die( 'Restricted Access' );
/**
 * Manifest creation routine
 *
 * @author      Alex Stolz
 * @package     Joomla
 * @subpackage  GoodRelations Module
 * @copyright   Copyright (C) 2012 Alex Stolz, Bundeswehr University Munich - All rights reserved.
 * @license     LGPL
 */

define('DS', DIRECTORY_SEPARATOR);
require_once(dirname(__FILE__).DS.'arc'.DS.'ARC2.php');
//require_once(realpath('..'.DS.'configuration.php'));

define("VERSION", "3.0.0beta");


// configure the ARC2 framework (used for SPARQL queries)
$conf = new JConfig(); // configuration data, get db information from it
$config_arc = array(
  // db
  'db_host' => $conf->host,
  'db_name' => $conf->db,
  'db_user' => $conf->user,
  'db_pwd' => $conf->password,
  // store name = table prefix
  'store_name' => $conf->dbprefix.'arc',
);

$store = ARC2::getStore($config_arc);
if (!$store->isSetUp()) {
    $store->setUp(); // create mysql tables
}
$store->query('LOAD <http://purl.org/goodrelations/v1#>');

$newj = false;
$param = "param";
if(version_compare(JVERSION, '1.6.0', 'ge')) {
	$newj = true;
	$param = "field";
}
/* creates a mod_goodrelations.xml file */
$manifest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
if($newj)
	$manifest .= "<extension type=\"module\" version=\"1.6.0\" method=\"upgrade\">\n";
else
	$manifest .= "<install type=\"module\" version=\"1.5.0\" method=\"upgrade\">\n";
$manifest .= "	<name>Semantic SEO</name>\n";
$manifest .= "	<description>GoodRelations e-commerce extension v".VERSION." for VirtueMart 2 - tuned for Rich Snippets in Google Search Engine Results!</description>\n";
$manifest .= "	<author>Alex Stolz and Martin Hepp, Bundeswehr University Munich</author>\n";
$manifest .= "	<authorEmail>development@stalsoft.com</authorEmail>\n";
$manifest .= "	<authorUrl>http://www.stalsoft.com/</authorUrl>\n";
$manifest .= "	<copyright>(C) ".date("Y", time())." E-Business and Web Science Research Group, GNU LGPL. All rights reserved.</copyright>\n";
$manifest .= "	<license>http://www.gnu.org/licenses/lgpl.html GNU LGPL</license>\n";
$manifest .= "	<creationDate>".date("d.m.Y", time())."</creationDate>\n";
$manifest .= "	<version>".VERSION."</version>\n";
$manifest .= "	<files>\n";
$manifest .= "		<filename module=\"mod_goodrelations\">mod_goodrelations.php</filename>\n";
$manifest .= "		<filename>create_manifest.php</filename>\n";
$manifest .= "		<filename>index.html</filename>\n";
$manifest .= "		<filename>helper.php</filename>\n";
$manifest .= "		<folder>tmpl</folder>\n";
$manifest .= "		<folder>arc</folder>\n";
$manifest .= "		<folder>img</folder>\n";
$manifest .= "		<folder>rdfa</folder>\n";
$manifest .= "	</files>\n";
//$manifest .= "  <params group=\"advanced\">\n";
if($newj)
	$manifest .= "  <config><fields name=\"params\"><fieldset name=\"basic\">\n";
else
	$manifest .= "  <params>\n";
$m_advanced .= "	<$param name=\"offer_individuals\" type=\"radio\" default=\"0\" label=\"Offer single items (i.e. no multiple sales for one type of product)\" description=\"Here you should specify whether you are offering single items only (e.g. second-hand article in Ebay vs. Amazon providing multiple items of a product)\">\n";
$m_advanced .= "		<option value=\"0\">No</option>\n";
$m_advanced .= "		<option value=\"1\">Yes</option>\n";
$m_advanced .= "    </$param>\n";
$m_advanced .= "	<$param name=\"offering_validity\" type=\"text\" default=\"1\" label=\"Offering validity duration\" description=\"Defines a global validity duration for offerings (in days). Though, product-specific expiration date has priority. A value of 0 means no expiration\" />\n";
$m_advanced .= "	<$param name=\"@spacer\" type=\"spacer\" label=\"\" default=\"\" description=\"\" />\n";
$m_advanced .= "	<$param name=\"uom_lwh\" type=\"list\" default=\"CMT\" label=\"Unit of measurement for dimensions\" description=\"Select the default unit of measurement for length, width and height dimensions\">\n";
$m_advanced .= "		<option value=\"MMT\">MMT (millimetre)</option>\n";
$m_advanced .= "		<option value=\"CMT\">CMT (centimetre)</option>\n";
$m_advanced .= "		<option value=\"DMT\">DMT (decimetre)</option>\n";
$m_advanced .= "		<option value=\"MTR\">MTR (metre)</option>\n";
$m_advanced .= "		<option value=\"HMT\">HMT (hectometre)</option>\n";
$m_advanced .= "		<option value=\"KMT\">KMT (kilometre)</option>\n";
$m_advanced .= "		<option value=\"INH\">INH (inch)</option>\n";
$m_advanced .= "		<option value=\"77\">77 (milli-inch)</option>\n";
$m_advanced .= "		<option value=\"FOT\">FOT (foot)</option>\n";
$m_advanced .= "	</$param>\n";
$m_advanced .= "	<$param name=\"uom_weight\" type=\"list\" default=\"GRM\" label=\"Unit of measurement for weight\" description=\"Select the default unit of measurement for describing product weights\">\n";
$m_advanced .= "		<option value=\"MGM\">MGM (milligram)</option>\n";
$m_advanced .= "		<option value=\"CGM\">CGM (centigram)</option>\n";
$m_advanced .= "		<option value=\"DG\">DG (decigram)</option>\n";
$m_advanced .= "		<option value=\"GRM\">GRM (gram)</option>\n";
$m_advanced .= "		<option value=\"DJ\">DJ (decagram)</option>\n";
$m_advanced .= "		<option value=\"KGM\">KGM (kilogram)</option>\n";
$m_advanced .= "		<option value=\"TNE\">TNE (tonne)</option>\n";
$m_advanced .= "		<option value=\"LBR\">LBR (pound)</option>\n";
$m_advanced .= "		<option value=\"GRN\">GRN (grain)</option>\n";
$m_advanced .= "		<option value=\"ONZ\">ONZ (ounce)</option>\n";
$m_advanced .= "	</$param>\n";
$m_advanced .= "	<$param name=\"@spacer\" type=\"spacer\" label=\"\" default=\"\" description=\"\" />\n";
$m_advanced .= "	<$param name=\"eligibleRegions\" type=\"text\" default=\"\" label=\"Eligible regions (example: DE,IT,AT,GB)\" description=\"Here ones can define the eligible regions for the product offerings. The country codes (ISO 3166-1 or ISO 3166-2) have to be comma-separated.\" />\n";

// from sparql
$configuration_done = 0; // be sure, that the user has at least once seen the goodrelations master configuration data
$elements = array(
	'BusinessFunction' => 'Business Function',
	'BusinessEntityType' => 'Eligible Customer Type',
	'DeliveryMethod' => 'Available Delivery Method',
	'PaymentMethod' => 'Accepted Payment Method',
	'WarrantyScope' => 'Warranty Promise'
);
foreach($elements as $search => $alias) {
	$search_id = strtolower($search);
	// sparql query
	$q = '
	PREFIX gr: <http://purl.org/goodrelations/v1#>
	SELECT ?'.$search_id.' ?description WHERE {
		{ ?'.$search_id.' a gr:'.$search.'; rdfs:comment ?description } UNION
		{ ?'.$search_id.' a ?subclass .
		  ?subclass rdfs:subClassOf gr:'.$search.' . }
	}
	';
	if ($rows = $store->query($q, 'rows')) {
		$m_advanced .= "	<$param name=\"@spacer\" type=\"spacer\" label=\"\" default=\"\" description=\"\" />\n";
		if(preg_match('/BusinessFunction/i', $search)) {
			$m_advanced .= "	<$param name=\"".$search."\" type=\"list\" default=\"Sell\" label=\"$alias\" description=\"Select a $search from the list\">\n";
			foreach ($rows as $row) {
				$item = preg_replace('/^.*#/', '', $row[$search_id]);
				$m_advanced .= "		<option value=\"$item\">$item</option>\n";
			}
			$m_advanced .= "	</$param>\n";
		}
		else {
			foreach ($rows as $row) {
				$item = preg_replace('/^.*#/', '', $row[$search_id]);
				if(preg_match('/WarrantyScope/i', $search)) $item_arr[] = $item;
				$m_advanced .= "	<$param name=\"".$search."_$item\" type=\"radio\" default=\"0\" label=\"$alias *$item*\" description=\"".($row['description']?htmlentities($row['description']):"Enable the $search $item")."\">\n";
				$m_advanced .= "		<option value=\"0\">No</option>\n";
				$m_advanced .= "		<option value=\"1\">Yes</option>\n";
				$m_advanced .= "	</$param>\n";
				if(preg_match('/WarrantyScope/i', $search)) {
					$m_advanced .= "	<$param name=\"".$item."_duration\" type=\"text\" default=\"24\" label=\"Warranty duration for $item (in months)\" description=\"If the upper warranty item is enabled, you can define here the warranty duration (in months).\" />\n";
				}
			}
		}
		$configuration_done = 1;
	}
}
//$m_advanced .= "  </params>\n";
//$m_basic = "  <params>\n";
$m_basic .= "	<$param name=\"configuration_done\" type=\"hidden\" default=\"$configuration_done\" />\n";
$m_basic .= "	<$param name=\"version\" type=\"hidden\" default=\"".VERSION."\" />\n";
$m_basic .= "	<$param name=\"ping_submission_base_uri\" type=\"hidden\" default=\"\" />\n";
//$m_basic .= "  </params>\n";
$manifest .= $m_basic.$m_advanced;
if($newj)
	$manifest .= "  </fieldset></fields></config>\n";
else
	$manifest .= "  </params>\n";
if($newj)
	$manifest .= "</extension>\n";
else
	$manifest .= "</install>\n";

// save installer file
$manifestFile = fopen(dirname(__FILE__).DS."mod_goodrelations.xml", "w+");
if ($manifestFile) {
	fputs($manifestFile, $manifest);
	fclose($manifestFile);
} else {
	echo "<div><b>Error creating manifest XML file (mod_goodrelations.xml) in module directory, please check write permissions.</b></div>\n";
}

unset($conf);
if ($store->isSetUp()) {
  $store->drop(); // drop mysql tables
  unset($store);
}

?>